home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et-2_2.lha / et2.2 / src / PrintDialog.C < prev    next >
C/C++ Source or Header  |  1990-12-04  |  6KB  |  281 lines

  1. //$PrintDialog$
  2. #include "PrintDialog.h"
  3.  
  4. #include "CheapText.h"
  5. #include "OrdColl.h"
  6. #include "ObjectTable.h"
  7. #include "Error.h"
  8.  
  9. #include "Window.h"
  10. #include "PrintPort.h"
  11. #include "Document.h"
  12. #include "FileDialog.h"
  13. #include "ObjList.h"
  14. #include "CollView.h"
  15. #include "Scroller.h"
  16. #include "Printer.h"
  17. #include "Alert_e.h"
  18. #include "NumItem.h"
  19. #include "BorderItems.h"
  20. #include "Buttons.h"
  21.  
  22. #include "System.h"
  23.  
  24. const int cNameMinWidth = 150;
  25. Printer *gPrinters[10];
  26. PrintDialog *gPrintManager= 0;
  27. bool gPrinting= FALSE;
  28.  
  29. extern void InitPrinters();
  30.  
  31. int ShowPrintDialog(VObject *v)
  32. {
  33.     if (gPrintManager == 0)
  34.     gPrintManager= new PrintDialog;
  35.     return gPrintManager->ShowPrintDialog(v);
  36. }
  37.  
  38. //---- PrintDialog -------------------------------------------------------------
  39.  
  40. const int cIdSaveToFile =   cIdFirstUser + 2,
  41.       cIdOptions    =   cIdFirstUser + 3,
  42.       cIdFrom       =   cIdFirstUser + 4,
  43.       cIdTo         =   cIdFirstUser + 5,
  44.       cIdShowGrid   =   cIdFirstUser + 6,
  45.       cIdPrint      =   cIdFirstUser + 7,
  46.       cIdPrinters   =   cIdFirstUser + 100;
  47.  
  48. MetaImpl(PrintDialog, (TP(saveDialog), TP(optionDialog), TP(scroller),
  49.     TP(collview), TP(printers), TP(current), TP(lastcurrent), TP(window),
  50.     TP(vobject), TP(from), TP(to), TP(title), TB(showgrid),
  51.     TB(lastshowgrid), T(lastfrom), T(lastto), T(lastprinter), 0));
  52.  
  53. PrintDialog::PrintDialog(char *t) : Dialog(t, eBWinBlock)
  54. {
  55.     title= t;
  56.     showgrid= FALSE;
  57.  
  58.     InitPrinters();
  59.     ObjectTable::AddRoot(this);
  60.     printers= new OrdCollection;
  61.     
  62.     for (int i= 0; gPrinters[i]; i++)
  63.     printers->Add(new TextItem(i, gPrinters[i]->GetName(), gSysFont, Point(4,0)));
  64.  
  65.     collview= new CollectionView(this, printers);
  66.     collview->SetMinExtent(Point(cNameMinWidth+20, 0));
  67.     collview->SetId(cIdPrinters);
  68.     collview->SetContainer(this);
  69.     current= gPrinters[0];
  70. }
  71.  
  72. PrintDialog::~PrintDialog()
  73. {
  74.     if (printers) {
  75.     printers->FreeAll();
  76.     SafeDelete(printers);
  77.     }
  78. }
  79.  
  80. void PrintDialog::DoSetDefaults()
  81. {
  82.     from->SetValue(1);
  83.     to->SetValue(999);
  84.     collview->SetSelection(Rectangle(0,0,1,1));
  85.     current= gPrinters[0];
  86. }
  87.  
  88. void PrintDialog::DoSave()
  89. {
  90.     lastfrom= from->GetValue();
  91.     lastto= to->GetValue();
  92.     lastprinter= collview->GetSelection().origin.y;
  93.     lastcurrent= current;
  94.     lastshowgrid= showgrid;  // joep@tnosoes
  95. }
  96.  
  97. void PrintDialog::DoRestore() 
  98. {
  99.     from->SetValue(lastfrom);
  100.     to->SetValue(lastto);
  101.     collview->SetSelection(Rectangle(0,lastprinter,1,1));    
  102.     current= lastcurrent;
  103.     showgrid= lastshowgrid;   // joep@tnosoes
  104.     // ((RadioButton*)((LabeledButton*)optionDialog->GetInterior())->GetItem(0,0))->SetState(showgrid);
  105. }
  106.  
  107. void PrintDialog::DoSetup()
  108. {
  109.     bool b= current != 0;
  110.     EnableItem(cIdSaveToFile, b);
  111.     EnableItem(cIdOptions, b && current->GetOptions());
  112.     EnableItem(cIdPrint, b && current->CanPrint());
  113. }
  114.  
  115. int PrintDialog::ShowPrintDialog(VObject *v)
  116. {
  117.     if (v) {
  118.     window= v->GetWindow();
  119.     oldvobject= vobject;
  120.     vobject= v;
  121.     if (v != oldvobject && oldvobject && showgrid)
  122.         oldvobject->ForceRedraw();
  123.     return ShowOnWindow(window);
  124.     }
  125.     return -1;
  126. }
  127.  
  128. VObject *PrintDialog::DoCreateDialog()
  129. {
  130.     //---- dialog parts ----
  131.     
  132.     scroller= new Scroller(collview, Point(cNameMinWidth, 16*4), cIdPrinters);
  133.  
  134.     // VObject *Options=
  135.     optionDialog=
  136.         new BorderItem("Options", new LabeledButton(cIdShowGrid, "show pages"));
  137.  
  138.     VObject *Actions=
  139.     new Cluster(cIdNone, (VObjAlign)(eVObjHLeft|eVObjHExpand), 8,
  140.         new ActionButton(cIdYes,        "Apply", TRUE),
  141.         new ActionButton(cIdPrint,      "Print"),
  142.         new ActionButton(cIdSaveToFile, "Save as ..."),
  143.         new ActionButton(cIdOptions,    "Options"),
  144.         new ActionButton(cIdCancel,     "Cancel"),
  145.         new ActionButton(cIdDefault,    "Default"),
  146.         0
  147.     );
  148.     
  149.     VObject *Pages=
  150.     new BorderItem("Pages",
  151.         new Cluster(cIdNone, (VObjAlign)(eVObjVBase|eVObjHExpand), 20,
  152.         new TextItem("From:"),
  153.         from= new NumItem(cIdFrom, 1, 1, 999, 3, 1),
  154.         new TextItem("To:"),
  155.         to= new NumItem(cIdTo, 999, 1, 999, 3, 1),
  156.         0
  157.         )
  158.     );
  159.     
  160.     //---- overall layout ----
  161.     return
  162.     new BorderItem(
  163.         new Cluster(cIdNone, eVObjVTop, 20,
  164.         new Cluster(cIdNone, eVObjHLeft, 20,
  165.             scroller,
  166.             optionDialog,
  167.             Pages,
  168.             0
  169.         ),
  170.         Actions,
  171.         0
  172.         ),
  173.         20, 0
  174.     );
  175. }
  176.  
  177. void PrintDialog::Control(int id, int p, void *v)
  178. {
  179.     switch (id) {
  180.     
  181.     case cIdShowGrid:
  182.     showgrid= !showgrid;
  183.     if (vobject)
  184.         vobject->ForceRedraw();
  185.     break;
  186.     
  187.     case cIdOptions:
  188.     if (current && current->GetOptions()) {
  189.         if (current->ShowOnWindow(window) == cIdOk) {
  190.         if (vobject)
  191.             vobject->ForceRedraw();
  192.         }
  193.     }
  194.     return;
  195.     
  196.     case cIdPrinters:
  197.     if (p == cPartCollSelect) {
  198.         current= gPrinters[(int) v];
  199.         DoSetup();
  200.     }
  201.     break;
  202.     
  203.     case cIdSaveToFile:
  204.     if (saveDialog == 0)
  205.         saveDialog= new FileDialog();
  206.     if (saveDialog->ShowInWindow(eFDWrite, window) == cIdOk) {
  207.         Print(saveDialog->FileName());
  208.         Dialog::Control(cIdYes, p, v);
  209.     }
  210.     break;
  211.     
  212.     case cIdPrint:
  213.     Print(0);
  214.     break;
  215.     }
  216.     Dialog::Control(id, p, v);
  217. }
  218.  
  219. void PrintDialog::ShowPageGrid(Rectangle, VObject *v)
  220. {
  221.     if (showgrid && !gPrinting && current && v) {
  222.     int x, y;
  223.     Point e= v->GetExtent(), ps= GetPageSize();
  224.  
  225.     GrSetPenSize(3);
  226.     GrSetPenPattern(ePatGrey50);
  227.     for (x= ps.x; x <= e.x; x+= ps.x)
  228.         GrLine(Point(x,0), Point(x,e.y));
  229.     for (y= ps.y; y <= e.y; y+= ps.y)
  230.         GrLine(Point(0,y), Point(e.x,y));
  231.     GrSetPenNormal();
  232.    }
  233. }
  234.  
  235. void PrintDialog::Print(char *name)
  236. {
  237.     PrintPort *newport= current->MakePrintPort(name);
  238.     if (newport) {
  239.     GrCursor oldcursor= GrSetWaitCursor();
  240.     PortDesc currport= GrGetPort();
  241.     PortDesc oldport= window->GetPortDesc();
  242.  
  243.     gPrinting= TRUE;
  244.     GrSetPort(newport);
  245.     window->SetPortDesc(newport);
  246.     
  247.     Point po, pe= GetPageSize();
  248.     Rectangle vr= vobject->contentRect;
  249.     int np= 1;
  250.     
  251.     for (po.y= 0; po.y <= vr.extent.y; po.y+= pe.y) {
  252.         for (po.x= 0; po.x <= vr.extent.x; po.x+= pe.x) {
  253.         if (np >= from->GetValue() && np <= to->GetValue()) {
  254.             if (TestInterrupt("printing"))
  255.             goto out;
  256.             newport->OpenPage(np);
  257.             GrSetClip(pe, -po);
  258.             GrSetPenNormal();
  259.             GrSetPattern(ePatBlack);
  260.             vobject->DrawAll(Rectangle(po, pe), FALSE);
  261.             newport->GiveHint(eHintFlush, 0, 0);
  262.             newport->ClosePage();
  263.         }
  264.         np++;
  265.         }
  266.     }
  267. out:
  268.     window->SetPortDesc(oldport);
  269.     gPrinting= FALSE;
  270.     
  271.     GrSetPort(currport);
  272.     GrSetCursor(oldcursor);
  273.     SafeDelete(newport);
  274.     }
  275. }
  276.  
  277. Point PrintDialog::GetPageSize()
  278. {
  279.     return current->GetPageSize();
  280. }
  281.